package com.zzyweb.rentalcarsys.utils;

import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

/**
 * @author zhiyi
 * @date 9/29/25
 **/
@Slf4j
@Component
public class EmailApi {
    @Resource
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;

    /*
    * subject 主题， content 内容 to 收件邮箱地址
    * */
    @SneakyThrows
    public boolean sendHtmlEmail(String subject, String content, String... to){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
        mimeMessageHelper.setSubject(subject);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setText(content,true);
        mimeMessageHelper.setTo(to);
        javaMailSender.send(mimeMessage);
        System.out.println("success");
        return true;
    }

}
